package idv.arthur.work;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ManipulateMpSquadHost extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public ManipulateMpSquadHost() {
        super();
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	doPost(request,response);
    }

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		Widgets wid = new Widgets();
		request.setCharacterEncoding( wid.UIcodeSet );
		response.setContentType("application/text");
		response.setCharacterEncoding( wid.UIcodeSet );
		
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String errPars = "}";
		final String currentUser = request.getParameter("currentUser");
		String strSQL = "SELECT COUNT(*) FROM mpsquadhosts WHERE bssid=? AND subsysid=? AND envid=? AND squadid=? AND  ip=? AND port=?";
		
		final String bssId = request.getParameter("bssId");
		String subSysId = request.getParameter("subSysId");
		String envId = request.getParameter("envId");
		String squadId = request.getParameter("squadId");
		String ip = request.getParameter("ip");
		int port = Integer.parseInt(request.getParameter("port"));

		String res = "";
		
		try {
			conn = wid.getConn(true);	
			ps = conn.prepareStatement(strSQL);
			ps.setString(1, bssId);
			ps.setString(2, subSysId);
			ps.setString(3, envId);
			ps.setString(4, squadId);
			ps.setString(5, ip);
			ps.setInt(6, port);
			rs = ps.executeQuery();
			rs.next();
			if ( rs.getInt(1) !=  0 ) {
				strSQL = "DELETE FROM mpsquadhosts WHERE bssid=? AND subsysid=? AND envid=? AND squadid=? AND  ip=? AND port=?";
				res = "刪除了"+squadId;
			} else {
				strSQL = "INSERT INTO mpsquadhosts (bssid,subsysid,envid,squadid,ip,port,creator,createdDateTime) VALUES (?,?,?,?,?,?,?,?)";
				res = "新增了"+squadId;
			}
			ps = conn.prepareStatement(strSQL);
			ps.setString(1, bssId);
			ps.setString(2, subSysId);
			ps.setString(3, envId);
			ps.setString(4, squadId);
			ps.setString(5, ip);
			ps.setInt(6, port);
			if (rs.getInt(1) ==  0) {
				ps.setString(7, currentUser);
				ps.setTimestamp(8, wid.currentTimeStamp());
			}
			ps.execute();
		} catch ( SQLException e) {
			wid.kem(conn, ps, e, wid.toEType("SQLException")+errPars, wid.isPrint2Console );
		} catch ( Exception e) {
			wid.kem(conn, ps, e, wid.toEType("Exception")+errPars, wid.isPrint2Console );
		} finally {
			try {
				if ( rs != null ) { rs.close(); }
				if ( ps!= null ) { ps.close(); }
				if ( conn!= null ) { conn.close(); }
			} catch (SQLException e) {
				System.out.println(wid.errProcess(e));
			}
		}
		
		response.getWriter().write(res);
	}
}